package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.StudentSignUpApplyDto;
import com.jyy.prefabricated.entity.OfflineCertificate;
import com.jyy.prefabricated.service.BosService;
import com.jyy.prefabricated.service.OfflineCertificateService;
import com.jyy.prefabricated.vo.OfflineCertificateVo;
import com.jyy.prefabricated.vo.StudentSignUpApplyVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 线下证书表(OfflineCertificate)表控制层
 *
 * @author makejava
 * @since 2021-09-10 10:39:52
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class OfflineCertificateController {
    /**
     * 服务对象
     */
    @Resource
    private OfflineCertificateService offlineCertificateService;

    @Autowired
    private BosService bosService;

    /**
     * 上传证书
     *
     * @param file 主键
     * @return 单条数据
     */
    @PostMapping("/offlineCertificate")
    public AjaxResult insert(@RequestParam(value = "file") MultipartFile file) throws IOException {
        log.debug("REST request to upload file to OSS ");
        String originalFilename = file.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
        String objectName = UUID.randomUUID().toString().replace("-", "") + substring;

        String url = bosService.putObject(file, objectName);
        String[] strArray = originalFilename.split("_");
        if (strArray.length != 2) {
            return AjaxResult.error("上传证书命名不正确，命名内容包含身份证和证书类型名，中间用下划线_分割", -1);
        }
        OfflineCertificate offlineCertificate = new OfflineCertificate();
        offlineCertificate.setUrl(url);
        offlineCertificate.setDeleted(false);
        offlineCertificate.setIdCard(strArray[0]);
        offlineCertificate.setTypeName(strArray[1]);
        OfflineCertificate result = offlineCertificateService.insert(offlineCertificate);
        return AjaxResult.success(result);
    }

    /**
     * 根据主键删除
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/offlineCertificate/{id}/{status}")
    public AjaxResult updateStatus(@PathVariable Integer id, @PathVariable Boolean status) {
        OfflineCertificate offlineCertificate = new OfflineCertificate();

        offlineCertificate.setDeleted(status);
        offlineCertificate.setId(id);
        offlineCertificateService.update(offlineCertificate);
        return AjaxResult.success();
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @GetMapping("/offlineCertificate/{id}")
    public AjaxResult selectById(@PathVariable Integer id) {
        OfflineCertificate result = offlineCertificateService.queryById(id);
        return AjaxResult.success(result);
    }


    /**
     * 查询列表
     *
     * @param dto
     * @return
     */
    @GetMapping("/offlineCertificate")
    public AjaxResult selectBy(OfflineCertificateVo dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<OfflineCertificateVo> offlineCertificateVo = offlineCertificateService.selectByParams(dto);
        PageInfo<OfflineCertificateVo> result = new PageInfo<>(offlineCertificateVo);
        return AjaxResult.success(result);
    }


}
